﻿using _2112A_2_MES._chubowen;
using _2112A_2_MES._XiangHe;
using _2112A_2_MES.ChuBoWens;
using _2112A_2_MES.WangWeiHaos.Inputs;
using _2112A_2_MES.WangWeiHaos.Outputs;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace _2112A_2_MES.ChuBoWen
{
    /// <summary>
    /// 物料产品表服务实现
    /// </summary>
    [ApiExplorerSettings(GroupName = "楚博文")]
    public class MditemServiceChu : ApplicationService, IMditemServiceChu
    {
        /// <summary>
        /// 客户仓储
        /// </summary>
        private readonly IRepository<MdItem, int> _mditemRepository;
        public MditemServiceChu(IRepository<MdItem, int> mditemRepository)
        {
            _mditemRepository = mditemRepository;
        }

        /// <summary>
        /// 获取物料产品列表
        /// </summary>
        /// <param name="itemCode">物料产品编码</param>
        /// <param name="itemName">物料产品名称</param>
        /// <returns>返回物料产品泛型集合</returns>
        public async Task<InterSelect<object>> GetMditemList(string? itemCode, string? itemName, int pageIndex, int pageSize)
        {
            var mditemList = await _mditemRepository.GetListAsync();
            if (!string.IsNullOrEmpty(itemCode))
            {
                mditemList = mditemList.Where(x => x.ItemCode.Contains(itemCode)).ToList();
            }
            if (!string.IsNullOrEmpty(itemName))
            {
                mditemList = mditemList.Where(x => x.ItemName.Contains(itemName)).ToList();
            }
            var totalCount = mditemList.Count;
            var pageList = mditemList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var productClassificationList = AutoMapperExt.MapToList<MdItem>(pageList);
            var data = new { TotalCount = totalCount, PageList = productClassificationList };
            if (mditemList != null)
            {
                return new InterSelect<object>(true, "0", data);
            }
            return new InterSelect<object>(false, "1", null);
        }

        /// <summary>
        /// 新增物料产品
        /// </summary>
        /// <param name="mditem"></param>
        /// <returns></returns>
        public async Task<int> AddMditem(MdItem mditem)
        {
            var result = await _mditemRepository.FindAsync(x => x.ItemCode == mditem.ItemCode);
            if (result == null)
            {
                mditem.CreateBy = "楚博文";
                mditem.CreateTime = DateTime.Now;
                await _mditemRepository.InsertAsync(mditem);
                return 1;
            }
            return 0;
        }

        /// <summary>
        /// 删除物料产品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteMditem(int id)
        {
            var result = await _mditemRepository.FindAsync(id);
            if (result!= null)
            {
                await _mditemRepository.DeleteAsync(id);
                return 1;
            }
            return 0;
        }

        /// <summary>
        /// 获取物料产品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<MdItem> GetMditem(int id)
        {
            var result = await _mditemRepository.FindAsync(id);
            if (result != null)
            {
                return result;
            }
            return null;
        }

        /// <summary>
        /// 修改物料产品
        /// </summary>
        /// <param name="mditem"></param>
        /// <returns></returns>
        public async Task<int> UpdateMditem(MditemDTO mditem)
        {
            //var result = await _mditemRepository.FindAsync(x => x.ItemCode == mditem.ItemCode);
            //if (result!= null)
            //{
            //    result.ItemCode = mditem.ItemCode;
            //    result.ItemName = mditem.ItemName;
            //    result.Specification = mditem.Specification;
            //    result.UnitOfMeasu = mditem.UnitOfMeasu;
            //    result.EnableFlag = mditem.EnableFlag;
            //    result.SafeStockFlag = mditem.SafeStockFlag;
            //    mditem.UpdateBy = "楚博文";
            //    mditem.UpdateTime = DateTime.Now;
            //    await _mditemRepository.UpdateAsync(mditem);
            //    return 1;
            //}
            //return 0;
            var result = await _mditemRepository.GetAsync(mditem.Id);
            result.ItemCode = mditem.ItemCode;
            result.ItemName = mditem.ItemName;
            result.Specification = mditem.Specification;
            result.UnitOfMeasu = mditem.UnitOfMeasu;
            result.EnableFlag = mditem.EnableFlag;
            result.SafeStockFlag = mditem.SafeStockFlag;
            result.UpdateBy = "小呆呆";
            result.UpdateTime = DateTime.Now;
            await _mditemRepository.UpdateAsync(result);
            return result != null ? 1 : 0;
        }
    }
}
